<?php

class Application_Model_DbTable_Incidents extends Zend_Db_Table_Abstract
{

	protected $_name = 'incidents';

	public function getIncident($id)
	{
		$id = (int)$id;
		$row = $this->fetchRow('id = ' . $id);
		if (!$row) {
			throw new Exception("Impossible de trouver l'incident $id");
		}
		return $row->toArray();
	}

	public function addIncident($materiel,$datePanne,$employe,$typePanne,$resumePanne,$details,$situation)
	{
		$data = array(
		 
        	'idMateriel'=> $materiel,
        	'datePanne' => $datePanne,
        	'idEmploye'=>$employe,
        	'typePanne' => $typePanne,
			'resumePanne' => $resumePanne,
        	'details' => $details, 
        	'idSituation' => $situation,       	
		 

		);
		$this->insert($data);
	}
	
 public function updateIncident($id,$materiel,$datePanne,$employe,$typePanne,$resumePanne,$details,$situation,$dateResolution,$cout, $solution)
    {
        $data = array(
            'idMateriel'=> $materiel,
        	'datePanne' => $datePanne,
        	'idEmploye'=>$employe,
        	'typePanne' => $typePanne,
        	'resumePanne' => $resumePanne,
        	'details' => $details, 
        	'idSituation' => $situation,
        	'dateResolution' => $dateResolution,
        	'cout' => $cout,
        	'solution' => $solution,
        );
        $this->update($data, 'id = '. (int)$id);
    }
    
public function changeSituationIncident($id,$situation,$dateResolution,$cout, $solution)
    {
        $data = array(
            'idSituation' => $situation,
        	'dateResolution' => $dateResolution,
        	'cout' => $cout,
        	'solution' => $solution,
        );
        $this->update($data, 'id = '. (int)$id);
    }
	
	public  function listCurrentIncident(){
		$select = $this->select()->setIntegrityCheck(false)
		->from(array ('i' => 'incidents'))
		->joinInner(array ('m' => 'materiels'),'i.idMateriel = m.id', array('nomMateriel' => 'nom','numeroPoste'=>'numeroPoste','etat'=>'etat'))
		->joinInner(array ('r' => 'reseaux'),'m.idReseau = r.id', array('raccourciReseau' => 'raccourci'))
		->joinInner(array ('s' => 'situations'),'i.idSituation = s.id', array('nomSituation' => 'nom'))
		->joinInner(array ('e' => 'employes'),'e.id = i.idEmploye', array('nomEmploye' => 'nom'))
		->where('etat=?','on service')
		->where('s.nom != ?','resolu')
		->order('i.datePanne DESC');
		 
		//echo $select;
		 
		$r= $this->fetchAll($select);


		return $r;
	}
	
public  function listAllIncident(){
		$select = $this->select()->setIntegrityCheck(false)
		->from(array ('i' => 'incidents'))
		->joinInner(array ('m' => 'materiels'),'i.idMateriel = m.id', array('nomMateriel' => 'nom','numeroPoste' => 'numeroPoste',))
		->joinInner(array ('r' => 'reseaux'),'m.idReseau = r.id', array('raccourciReseau' => 'raccourci'))
		->joinInner(array ('s' => 'situations'),'i.idSituation = s.id', array('nomSituation' => 'nom'))
		->joinInner(array ('e' => 'employes'),'e.id = i.idEmploye', array('nomEmploye' => 'nom'))
		->order('i.datePanne DESC');
		 
		//echo $select;
		 
		$r= $this->fetchAll($select);


		return $r;
	}
	
	public function statByMonthIncident($year){	
		
 		$stmt = $this->getAdapter()->fetchAll("CALL incidentsByMonth(?)",array($year));
		
		return $stmt;
 		
	}
	
	public function statByEmployeIncident(){
		$select =
		$this->select()->setIntegrityCheck(false)
		->from(array ('i' => 'incidents'),array('nbIncidents'=>'COUNT(*)'))
		->joinInner(array ('e' => 'employes'),'i.idEmploye = e.id', array('idEmploye'=>'e.id', 'nomEmploye'=>'e.nom', 'prenomEmploye' => 'e.prenom'))		
		->group('idEmploye')
		->order('nbIncidents DESC');
		
		$r = $this->fetchAll($select);
		return $r;
	}

}

